package com.shizhanzhe.gxb.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.BaseFragment;
import com.shizhanzhe.gxb.home.activity.HighCommissionActivity;
import com.shizhanzhe.gxb.home.activity.MallGoodsActivity;
import com.shizhanzhe.gxb.home.activity.PDDDetailActivity;
import com.shizhanzhe.gxb.home.activity.RecommendActivity;
import com.shizhanzhe.gxb.home.activity.StoreActivity;
import com.shizhanzhe.gxb.home.activity.TopGoodsActivity;
import com.shizhanzhe.gxb.home.adapter.MerchantlistAdapter;
import com.shizhanzhe.gxb.home.adapter.TopgoodsAdapter;
import com.shizhanzhe.gxb.home.bean.Merchantlist;
import com.shizhanzhe.gxb.home.bean.Topgoods;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.gxb.widget.GlideImageLoader;
import com.shizhanzhe.gxb.widget.MyGridView;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 * @author YJ
 * @date 2019/1/17
 */
public class RecommendFragment extends BaseFragment {
    @BindView(R.id.banner)
    Banner banner;
//    @BindView(R.id.gridview)
    MyGridView gridview;
    @BindView(R.id.listview)
    MyGridView listview;
    private Activity mContext;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        gridview = view.findViewById(R.id.gridview);
        List<Integer> imgs = new ArrayList<Integer>();
        imgs.add(R.mipmap.banner_rx);
        imgs.add(R.mipmap.banner_sy);
        imgs.add(R.mipmap.banner_vip);
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(imgs);
//        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
        banner.setBannerAnimation(Transformer.CubeIn);
        banner.start();
        merchantlist();
        setListener();
    }

    @OnClick({R.id.iv_free, R.id.iv_today, R.id.iv_empty, R.id.haibao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_free:
                startActivity(new Intent(getContext(), RecommendActivity.class).putExtra("type", 0));
                break;
            case R.id.iv_today:
                startActivity(new Intent(getContext(), RecommendActivity.class).putExtra("type", 1));
                break;
            case R.id.iv_empty:
                startActivity(new Intent(getContext(), RecommendActivity.class).putExtra("type", 2));
                break;
            case R.id.haibao:
                mListener.govip();
                break;
            default:
                break;
        }
    }

    private void setListener() {
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(getContext(), TopGoodsActivity.class).putExtra("type",1));
                        break;
                    case 1:
                        //startActivity(new Intent(getContext(), TopGoodsActivity.class).putExtra("type",2));
                        startActivity(new Intent(getContext(), HighCommissionActivity.class));
                        break;
                    case 2:
                        mListener.govip();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void merchantlist() {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.merchantlist(), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        final Merchantlist merchantlist = new Gson().fromJson(json, Merchantlist.class);
                        if (merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list().size() > 0) {
                            if (merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list().size()>=9){
                                merchantlist.getData().getMerchant_list_response().setMall_search_info_vo_list(merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list().subList(0,8));
                            }
//                            if (merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list().size()%3 != 0) {
                                merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list().add(new Merchantlist.DataBean.MerchantListResponseBean.MallSearchInfoVoListBean(R.mipmap.load_img_error,0, "更多"));
//                            }
                            gridview.setAdapter(new MerchantlistAdapter(merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list(), getContext()));
                            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Merchantlist.DataBean.MerchantListResponseBean.MallSearchInfoVoListBean bean = merchantlist.getData().getMerchant_list_response().getMall_search_info_vo_list().get(i);
                                    if (bean.getMall_id() != 0) {
                                        startActivity(new Intent(getContext(), MallGoodsActivity.class)
                                                .putExtra("name", bean.getMall_name()).putExtra("mall_id", bean.getMall_id()));
                                    } else {
                                        startActivity(new Intent(getContext(), StoreActivity.class));
                                    }
                                }
                            });
                        }
                    } catch (Exception e) {
                        Log.d("_____",e.toString());
                        Toast.makeText(mContext, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.topgoods(30,1), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        final Topgoods topgoods = new Gson().fromJson(json, Topgoods.class);
                        if (topgoods.getData().getTop_goods_list_get_response().getList().size() > 0) {
                            final List<Topgoods.DataBean.TopGoodsListGetResponseBean.ListBean> beanList = new ArrayList<>();
                            for (Topgoods.DataBean.TopGoodsListGetResponseBean.ListBean bean : topgoods.getData().getTop_goods_list_get_response().getList()){
                                double qhprice=((double) bean.getMin_group_price()-(double)bean.getCoupon_discount())/100;
                                if (qhprice<=10 && bean.getCoupon_discount()/100 != 0){
                                    beanList.add(bean);
                                }
                            }
                            listview.setAdapter(new TopgoodsAdapter(beanList, getContext()));
                            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Topgoods.DataBean.TopGoodsListGetResponseBean.ListBean bean = beanList.get(i);
                                    startActivity(new Intent(getContext(), PDDDetailActivity.class)
                                            .putExtra("goods_id", bean.getGoods_id()));
                                }
                            });
                        }
                    } catch (Exception e) {
                        Log.d("____",e.toString());
                        Toast.makeText(mContext, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void initView() {

    }
    private RecommendListener mListener;

    public RecommendListener getListener() {
        return mListener;
    }

    public void setListener(RecommendListener listener) {
        mListener = listener;
    }

    public interface RecommendListener{
        void govip();
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext= activity;
    }

}
